import { spawn } from 'child_process';
import path from 'path';

const comfyuiPath = 'E:\\sd\\ComfyUI_windows_portable';
const comfyuiCmd = `${comfyuiPath}\\python_embeded\\python.exe`;
const comfyuiArgs = ['-s', `${comfyuiPath}\\ComfyUI\\main.py`, '--windows-standalone-build'];

function startComfyUI() {
  const process = spawn(comfyuiCmd, comfyuiArgs, {
    stdio: 'inherit',
    shell: true
  });

  process.on('exit', (code) => {
    console.log(`ComfyUI进程退出，代码: ${code}`);
    console.log('5秒后尝试重启...');
    setTimeout(startComfyUI, 5000);
  });

  process.on('error', (err) => {
    console.error('启动ComfyUI失败:', err);
    console.log('10秒后尝试重启...');
    setTimeout(startComfyUI, 10000);
  });
}

startComfyUI();
